home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Utilities Professional 1-1500
/
Utilities Professional 1-1500 (1994)(WPD)[!].iso
/
12511500
/
var1451.dms
/
var1451.adf
/
ParallelDevice
/
Example3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-27
|
20KB
|
504 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Parallel Device Tulevagen 22 */
/* File: Example3.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-04-26 */
/* Version: 1.00 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* This example is rather similar to Example 1, but this time */
/* we do not wait for the parallel port to complete our request. */
/* We are also trying to read and write at the same time. To be */
/* able to do several requests simultaneously we need one request */
/* block for each command. In this example we use three separate */
/* request blocks. */
#include <exec/types.h> /* STRPTR */
#include <devices/parallel.h> /* Parallel Device */
/* Size of our data buffer: */
#define MY_BUFFER_SIZE 200
/* Parallel flags: (Check for end-of-file characters.) */
#define PARALLEL_FLAGS PARF_EOFMODE
/* Additional flags: (Nothing) */
#define ADDITIONAL_FLAGS 0
/* Declare a pointer to our reply port: */
struct MsgPort *replymp = NULL;
/* Declare pointers to our parallel request blocks: */
/* One is used for reading, the other for writing, and */
/* finally the last one is used for other commands. */
struct IOExtPar *parallel_req_read = NULL; /* Read */
struct IOExtPar *parallel_req_write = NULL; /* Write */
struct IOExtPar *parallel_req_command = NULL; /* Command */
/* Store the parallel device error here: */
UWORD parallel_dever = TRUE;
/* Declare two data buffers. The first one will contain the */
/* information we want to send, and the other will be filled */
/* with all data we have collected: */
BYTE read_buffer[ MY_BUFFER_SIZE ];
BYTE write_buffer[ MY_BUFFER_SIZE ];
/* Declare our functions: */
/* Our main function: */
void main();
/* Clears and removes everything nice and neatly: */
void clean_up( UBYTE error, STRPTR text );
/* Sets the parallel parameters: */
UBYTE SetParParams(
struct IOExtPar *ioreq,
UBYTE parallel_flags,
ULONG extended_flags,
UBYTE *eof_chars
);
/* Explains error messages: */
void ParError( UBYTE error );
/* Sends data to the parallel device without waiting: */
void ParWriteNoWait(
struct IOExtPar *ioreq,
BYTE *data,
ULONG length
);
/* Collects data from the parallel device without waiting: */
void ParReadNoWait(
struct IOExtPar *ioreq,
BYTE *data,
ULONG length
);
void main()
{
/* Error number: */
UBYTE error;
/* Byte pointers, used to copy the request blocks: */
BYTE *r_ptr;
BYTE *w_ptr;
BYTE *c_ptr;
/* Loop variable: */
int loop;
/* The eight end-of-file characters: */
/* They MUST be in descending order! */
UBYTE eof_char[8]={ 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00 };
/* Get a reply port: (No name, priority 0) */
replymp = (struct MsgPort *)
CreatePort( NULL, 0 );
if( !replymp )
clean_up( 0, "Could not create the reply port!" );
/* Create request block "Read": */
parallel_req_read = (struct IOExtPar *)
CreateExtIO( replymp, sizeof( struct IOExtPar ) );
if( !parallel_req_read )
clean_up( 0, "Not enough memory for the request block Read!" );
/* Create request block "Write": */
parallel_req_write = (struct IOExtPar *)
CreateExtIO( replymp, sizeof( struct IOExtPar ) );
if( !parallel_req_write )
clean_up( 0, "Not enough memory for the request block Write!" );
/* Create request block "Command": */
parallel_req_command = (struct IOExtPar *)
CreateExtIO( replymp, sizeof( struct IOExtPar ) );
if( !parallel_req_command )
clean_up( 0, "Not enough memory for the request block Command!" );
/* Open the parallel device for the read request: */
parallel_dever = OpenDevice( PARALLELNAME, 0, parallel_req_read, 0 );
if( parallel_dever )
clean_up( parallel_dever, "Could not open the Parallel Device!" );
/* Since we can not open the parallel device any more */
/* for the other request (we use exclusive access), we */
/* have to copy the whole read request block into the */
/* write and command request blocks. */
/* Get the start address of all request blocks: */
r_ptr = (BYTE *) parallel_req_read;
w_ptr = (BYTE *) parallel_req_write;
c_ptr = (BYTE *) parallel_req_command;
/* Copy the request block, byte by byte: */
for( loop=0; loop < sizeof( struct IOExtPar ); loop++ )
{
/* Copy one byte: */
*w_ptr = *r_ptr; /* Write */
*c_ptr = *r_ptr; /* Command */
/* Step one byte foreward: */
w_ptr++;
r_ptr++;
c_ptr++;
}
/* Set the parallel device's parameters: */
error = (UBYTE) SetParParams(
parallel_req_write, /* Pointer to our parallel request block. */
PARALLEL_FLAGS, /* Parallel flags. */
ADDITIONAL_FLAGS, /* Additional flags. */
eof_char /* Pointer to an array of eight end-of-file chr. */
);
/* OK? */
if( error )
clean_up( error, "Could not set the parallel parameters!" );
/* Send 0 bytes to the parallel port and return immediately: */
/* (Since I do not know what you have connected to your */
/* parallel port, it is best not to do anything.) */
ParWriteNoWait( parallel_req_write, write_buffer, 0 );
/* Collect 0 bytes from the parallel port and return immediately: */
ParReadNoWait( parallel_req_read, read_buffer, 0 );
/* Do whatever you want... */
/* When you are using asynchronious commands you have to be */
/* careful with cleaning up. All requests you have started */
/* MUST have been completed or aborted before you may close */
/* the device. All reply massages must also be removed from */
/* the reply port. */
/* */
/* There exist several different ways on how to wait for */
/* requests to be completed. In example 2 we used the */
/* function CheckIO(), and Remove(). In this example will */
/* we use the WaitIO() which puts our program to sleep and */
/* will first wake up when the request have been completed. */
/* WaitIO() will automatically remove the reply messages, */
/* and if the request have already been competed it will */
/* immediately return. */
/* */
/* The difference between a busy wait and a task sleep is */
/* that your program can do other things while waiting if */
/* you are using the busy wait. The task sleep should be */
/* used if you do not want to do anything while waiting. */
/* */
/* NOTE! Do NOT use a busy wait if you can manage with a */
/* task sleep! Computer time should always be used with */
/* care. */
/* */
/* We have three request blocks that has to be looked after. */
/* The "Command" block was only used by the SetParParams() */
/* function and was used as a synchronous request. [DoIO()] */
/* We will therefore (nor should we) wait for it to be */
/* completed or try to remove any reply message. The two */
/* other request blocks were on the other hand asynchronous, */
/* and we must therefore both wait for them to be completed */
/* and make sure that the reply messages are removed. The */
/* WaitIO() function will do both of these things. */
/* */
/* NOTE! Do NOT try to wait for a reuest that has not been */
/* started [either by a SendIO() or BeginIO()]. */
/* Task sleep... (It does not matter in which order we wait.) */
WaitIO( parallel_req_read );
WaitIO( parallel_req_write );
/* Clean up and quit: */
clean_up( 0, "The End!" );
}
/* Close and return everything that has been */
/* opened and allocated before we quit: */
void clean_up( UBYTE error, STRPTR text )
{
/* Print some information about the problem: */
if( error )
ParError( error );
/* Close the Parallel Device: */
if( !parallel_dever )
CloseDevice( parallel_req_read );
/* Deallocate the parallel "read" request block: */
if( parallel_req_read )
DeleteExtIO( parallel_req_read, sizeof( struct IOExtPar ) );
/* Deallocate the parallel "write" request block: */
if( parallel_req_write )
DeleteExtIO( parallel_req_write, sizeof( struct IOExtPar ) );
/* Deallocate the parallel "command" request block: */
if( parallel_req_command )
DeleteExtIO( parallel_req_command, sizeof( struct IOExtPar ) );
/* Remove the replyport: */
if( replymp )
DeletePort( replymp);
/* Print the message: */
printf( "\n%s\n", text );
/* Quit: */
exit( 0 );
}
/* SetParParams() sets the parallel parameters. It initializes a IOExtPar */
/* structure, and does a PDCMD_SETPARAMS commad. If everything is OK it */
/* returns NULL, else an error number is returned. */
/* */
/* Synopsis: er = SetParParams( io, bl, br, bt, rl, wl, sl, sf, ef, chr ); */
/* */
/* er: (UBYTE) SetParParams() returns 0 if everything was OK, else */
/* an error value is returned. See function ParError() for more */
/* information. */
/* */
/* io: (struct IOExtPar *) Pointer to the parallel request block you */
/* want to initialize. */
/* */
/* PARF_RAD_BOOGIE Not supported by the parallel device for the */
/* moment. */
/* */
/* PARF_SHARED Set this flag if you want to allow other */
/* tasks running at the same time to use the */
/* parallel device. The default is exclusive- */
/* access. (If some other task is using the */
/* parallel device with the shared bit set, and */
/* you call this function with exclusive access, */
/* your request will fail.) */
/* */
/* PARF_EOFMODE Set this flag if you want to check for end of */
/* file characters. (You may use up to eight end */
/* of file characters, which are specified */
/* below.) */
/* */
/* ef: (ULONG) Not supported by the parallel device for the moment. */
/* */
/* chr: (UBYTE *) Pointer to an array containing eight end-of-file */
/* characters. If the parallel flag "PARF_EOFMODE" is set, the */
/* parallel device will check each character which is sent or */
/* received, and if it matches one of the end-of-file characters */
/* the read/wite request is terminated. */
UBYTE SetParParams(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
UBYTE parallel_flags, /* Parallel flags. */
ULONG extended_flags, /* Additional parallel flags. */
UBYTE *eof_chars /* Pointer to an array containing eight end-of- */
/* file characters. */
)
{
int loop; /* Used in the loop. */
UBYTE *ptr; /* Unsigned byte pointer. */
/* Set parallel flags: */
ioreq->io_ParFlags = parallel_flags;
/* Set additional flags: */
ioreq->io_PExtFlags = extended_flags;
/* Get the address of the IOTArray: */
ptr = (UBYTE *) &(ioreq->io_PTermArray);
/* Set all eight end of file characters: */
for( loop=0; loop < 8; loop++ )
{
/* Copy character after character: */
*ptr = eof_chars[ loop ];
/* Step one byte foreward: */
ptr++;
}
/* All values have now been set, lets do a PDCMD_SETPARAMS request: */
ioreq->IOPar.io_Command = PDCMD_SETPARAMS;
/* Do our request, and when complete return 0 if */
/* OK, else an error value: */
return( (UBYTE) DoIO( ioreq ) );
}
/* ParError() tells the user what went wrong. You give it the error code */
/* you received, and ParError() will print a short description of the */
/* problem. Useful when debugging. */
/* */
/* Synopsis: ParError( error ); */
/* */
/* error: (UBYTE) The error value you want to have explained. */
void ParError( UBYTE error )
{
switch( error )
{
case ParErr_DevBusy:
printf( "Some other task is already using the parallel Device!\n" );
break;
case ParErr_BufTooBig:
printf( "The parallel buffer is too big! (?)\n" );
break;
case ParErr_InvParam:
printf( "Invalid parameters!\n" );
break;
case ParErr_LineErr:
printf( "Line error, check all cables!\n" );
break;
case ParErr_NotOpen:
printf( "The Parallel Device is not open!\n" );
break;
case ParErr_PortReset:
printf( "Someone has resetted the Parallel Device!\n" );
break;
case ParErr_InitErr:
printf( "Could not initialize the Parallel Device!\n" );
break;
default:
printf( "An unknown error was reported! Error nr: %d\n", error );
}
}
/* ParWriteNoWait() sends some data to the Parallel Port, but returns */
/* immediately. You only have to give it a pointer to the data you */
/* want to write and tell it how many bytes you want to transfer. */
/* Since it does not wait for the request to be completed, you have */
/* to take care of removing the message yourself. Note that all */
/* requests that have been started must be completed or aborted */
/* before your program may close the parallel device. */
/* */
/* Synopsis: error = ParWriteNoWait( io, data, length ); */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized parallel */
/* request block. */
/* */
/* data: (BYTE *) Pointer to the first byte of the data you want */
/* to send (write). */
/* */
/* length: (ULONG) How many bytes you want to transfer. If you want */
/* to continue to send data until we have received an end- */
/* of-file character, set the length to -1. (Note that the */
/* parallel device will then ONLY stop when it receives one */
/* of the specified end-of-file characters.) */
void ParWriteNoWait(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
BYTE *data, /* Pointer to the data you want to send. */
ULONG length /* The length of the data you want to send. */
)
{
/* We want to send (write) some data: */
ioreq->IOPar.io_Command = CMD_WRITE;
/* Give the start address of our data: */
ioreq->IOPar.io_Data = (APTR) data;
/* Set the length of the message: */
ioreq->IOPar.io_Length = length;
/* Do our request and return immediately: */
SendIO( ioreq );
}
/* ParReadNoWait() reads some data from the Parallel Port, but returns */
/* immediately. You only have to give it a pointer to some memory */
/* where the data should be stored, and tell it how many bytes you */
/* want to read. Since it does not wait for the request to be */
/* completed, you have to take care of removing the message yourself. */
/* Note that all requests that have been started must be completed or */
/* aborted before your program may close the parallel device. */
/* */
/* */
/* Synopsis: error = ParReadNoWait( io, data, length ); */
/* */
/* io: (struct IOExtPar *) Pointer to an initialized parallel */
/* request block. */
/* */
/* data: (BYTE *) Pointer to the memory buffer where you want to */
/* store all data. */
/* */
/* length: (ULONG) How many bytes you want to read. If you want to */
/* continue to send data until we have received an end-of- */
/* file character, set the length to -1. (Note that the */
/* parallel device will then ONLY stop when it receives one */
/* of the end-of-file characters.) */
void ParReadNoWait(
struct IOExtPar *ioreq, /* Pointer to our parallel request block. */
BYTE *data, /* Where the data should be placed. */
ULONG length /* How many bytes you want to read. */
)
{
/* We want to read some data: */
ioreq->IOPar.io_Command = CMD_READ;
/* Give the start address of our data: */
ioreq->IOPar.io_Data = (APTR) data;
/* Set how many bytes you want to read: */
ioreq->IOPar.io_Length = length;
/* Do our request and return immediately: */
DoIO( ioreq );
}